package com.xca.znb.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.adapter.SearchAdapter;
import com.xca.znb.entity.Goods;
import com.xca.znb.model.impl.MyGoodsImpl;
import com.xca.znb.presenter.MyGoodsPresenter;

import java.util.List;

public class MyGoodsActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView Back ;
    private TextView shelves , off_shelves ;
    private ListView listGoods ;

    private List<Goods> goodsList ;
    private SearchAdapter adapter ;
    private MyGoodsPresenter presenter ;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_goods_activity);
        Back = findViewById(R.id.my_publish_goods_back);
        shelves = findViewById(R.id.my_publish_goods_shelves);
        off_shelves = findViewById(R.id.my_publish_goods_off_shelves);
        listGoods = findViewById(R.id.my_publish_goods_list);
        shelves.setSelected(true);
        shelves.setOnClickListener(this);
        off_shelves.setOnClickListener(this);

        MyGoodsImpl myGoods = new MyGoodsImpl(this);
        presenter = new MyGoodsPresenter(myGoods);

        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");

       goodsList = presenter.getGoodsOnByPhone(phone) ;
        adapter = new SearchAdapter(MyGoodsActivity.this , goodsList);
        listGoods.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        listGoods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView showId = view.findViewById(R.id.searchItem_goods_show_goodsId) ;
                String sId = showId.getText().toString();
                Intent intent = new Intent(MyGoodsActivity.this , GoodsManager.class) ;
                intent.putExtra("goodsManagerId" , sId) ;
                startActivity(intent);
            }
        });

        Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.my_publish_goods_shelves){
            shelves.setSelected(true);
            off_shelves.setSelected(false);

            SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
            String phone = sharedPreferences.getString("phone" , "");
            goodsList = presenter.getGoodsOnByPhone(phone) ;
            adapter = new SearchAdapter(MyGoodsActivity.this , goodsList);
            listGoods.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } else if (v.getId() == R.id.my_publish_goods_off_shelves) {
            shelves.setSelected(false);
            off_shelves.setSelected(true);

            SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
            String phone = sharedPreferences.getString("phone" , "");
            goodsList = presenter.getGoodsOffByPhone(phone) ;
            adapter = new SearchAdapter(MyGoodsActivity.this , goodsList);
            listGoods.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 刷新数据
        if (shelves.isSelected()){
            SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
            String phone = sharedPreferences.getString("phone" , "");
            goodsList = presenter.getGoodsOnByPhone(phone) ;
            adapter = new SearchAdapter(MyGoodsActivity.this , goodsList);
            listGoods.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } else if (off_shelves.isSelected()) {
            SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
            String phone = sharedPreferences.getString("phone" , "");
            goodsList = presenter.getGoodsOffByPhone(phone) ;
            adapter = new SearchAdapter(MyGoodsActivity.this , goodsList);
            listGoods.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    }

}
